<template>
  <view class="container">
    <view class="form-content">
      <uni-forms ref="form" :model="user" labelWidth="80px">
        <view class="form-item-wrapper">
          <uni-forms-item label="用户昵称" name="nickName">
            <uni-easyinput v-model="user.nickName" placeholder="请输入昵称" />
          </uni-forms-item>
        </view>
        <view class="form-item-wrapper">
          <uni-forms-item label="手机号码" name="phonenumber">
            <uni-easyinput v-model="user.phonenumber" placeholder="请输入手机号码" />
          </uni-forms-item>
        </view>
        <view class="form-item-wrapper">
          <uni-forms-item label="邮箱" name="email">
            <uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
          </uni-forms-item>
        </view>
        <view class="form-item-wrapper">
          <uni-forms-item label="性别" name="sex" required>
            <uni-data-checkbox v-model="user.sex" :localdata="sexs" />
          </uni-forms-item>
        </view>
      </uni-forms>
      <u-button type="warning" @click="submit">保存</u-button>
    </view>
  </view>
</template>

<script>
  import { getUserProfile } from "@/api/system/user"
  import { updateUserProfile } from "@/api/system/user"

  export default {
    data() {
      return {
        user: {
          nickName: "",
          phonenumber: "",
          email: "",
          sex: ""
        },
        sexs: [{
          text: '男',
          value: "0"
        }, {
          text: '女',
          value: "1"
        }],
        rules: {
          nickName: {
            rules: [{
              required: true,
              errorMessage: '用户昵称不能为空'
            }]
          },
          phonenumber: {
            rules: [{
              required: true,
              errorMessage: '手机号码不能为空'
            }, {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码'
            }]
          },
          email: {
            rules: [{
              required: true,
              errorMessage: '邮箱地址不能为空'
            }, {
              format: 'email',
              errorMessage: '请输入正确的邮箱地址'
            }]
          }
        }
      }
    },
    onLoad() {
      this.getUser()
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      getUser() {
        getUserProfile().then(response => {
          this.user = response.data
        })
      },
      submit(ref) {
        this.$refs.form.validate().then(res => {
          updateUserProfile(this.user).then(response => {
            this.$modal.msgSuccess("修改成功")
			uni.$emit('updateUserSuccess')
			uni.navigateBack({
				delta: 1
			});
          })
        })
      }
    }
  }
</script>

<style lang="scss">
page {
  background-color: #f5f5f5;
}

.container {
  padding: 20rpx;
  
  .form-content {
    background-color: #fff;
    border-radius: 12rpx;
    overflow: hidden;

    .form-item-wrapper {
      position: relative;
      padding: 0 30rpx;
      
      &::after {
        content: '';
        position: absolute;
        left: 30rpx;
        right: 30rpx;
        bottom: 0;
        height: 1px;
        background-color: #eee;
        transform: scaleY(0.5);
      }
      
      &:last-child::after {
        display: none;
      }

      :deep(.uni-forms-item) {
        padding: 24rpx 0;
        
        .uni-forms-item__label {
          font-size: 28rpx;
          color: #333;
        }
        
        .uni-forms-item__content {
          .uni-easyinput__content {
            height: 44rpx;
            border: none;
            background: none;
          }
          
          .uni-data-checklist {
            .checklist-group {
              justify-content: flex-end;
              
              .checklist-box {
                padding: 0;
                margin-left: 40rpx;
              }
            }
          }
        }
      }
    }

    .submit-btn {
      margin: 60rpx 30rpx;
      height: 88rpx;
      line-height: 88rpx;
      font-size: 32rpx;
      border-radius: 44rpx;
      background: linear-gradient(to right, #3c9cff, #5dabff);
      box-shadow: 0 10rpx 20rpx rgba(60,156,255,0.3);
      border: none;
      
      &::after {
        border: none;
      }
    }
  }
}
</style>
